package com.own.business.resource.core.model;

import com.own.business.resource.core.model.base.BaseUploadModel;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.exception.ExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

/**
 * UploadModel
 *
 * @author chenxueli
 * @date 2023-07-07 16:51:08
 */
@Slf4j
public class UploadModel extends BaseUploadModel {

    private final UploadMultipartModel multipartFile;

    private final UploadFileModel file;

    /**
     * spring 文件上传的接收对象
     *
     * @param multipartFile 文件信息
     */
    public UploadModel(MultipartFile multipartFile) {
        this.multipartFile = new UploadMultipartModel(multipartFile);
        this.file = null;
        setInfo(this.multipartFile);
    }

    /**
     * spring 文件上传的接收对象
     *
     * @param multipartFile 文件信息
     * @param md5           是否计算md5
     */
    public UploadModel(MultipartFile multipartFile, boolean md5) {
        this.multipartFile = new UploadMultipartModel(multipartFile, md5);
        this.file = null;
        setInfo(this.multipartFile);
    }

    /**
     * 文件上传
     *
     * @param path 文件路径
     */
    public UploadModel(String path) {
        this.multipartFile = null;
        this.file = new UploadFileModel(path);
        setInfo(this.file);
    }

    /**
     * 文件上传
     *
     * @param file 文件信息
     */
    public UploadModel(File file) {
        this.multipartFile = null;
        this.file = new UploadFileModel(file);
        setInfo(this.file);
    }

    /**
     * 文件上传
     *
     * @param file 文件信息
     * @param md5  是否计算md5
     */
    public UploadModel(File file, boolean md5) {
        this.multipartFile = null;
        this.file = new UploadFileModel(file, md5);
        setInfo(this.file);
    }

    /**
     * 设置文件流信息
     *
     * @return 文件流信息
     */
    @Override
    public InputStream setInputStream() {
        if (multipartFile != null) {
            return multipartFile.setInputStream();
        }
        if (file != null) {
            return file.setInputStream();
        }
        throw new BusinessSimpleException(ExceptionEnum.Resource.INPUT_STREAM_WRROR);
    }

}
